#!/usr/bin/env bash

# This script helps me to take screenshots and record my desktop.
# Dependencies: wofi, wf-recorder, slurp, grim 

declare -a options=(
"Record screencast"
"Stop screencast"
"Take screenshot"
)

choice=$(printf '%s\n' "${options[@]}" | wofi --style=$HOME/.config/wofi.css --dmenu -i -p 'Choose script:' 2>/dev/null )

if [ "$choice" = "Take screenshot" ]; then 
    geometry=$(slurp)
    notify-send "Screenshot in 3 seconds..."
    rm -rf $HOME/stuff/screen.png
    sleep 3
    grim -g "$geometry" - > $HOME/stuff/screen.png
    wl-copy $HOME/stuff/screen.png -t image/png
    notify-send "Screenshot taken!"
    read -r W H <<< $(identify -format "%w %h\n" ~/stuff/screen.png)
    if [ "$W" -gt "300" ] || [ "$H" -gt "300" ]; then
        pqiv ~/stuff/screen.png -z 0.45 &
    else 
        pqiv ~/stuff/screen.png &
    fi 
    sleep 3
    pkill pqiv 
elif [ "$choice" = "Record screencast" ]; then
    rm -rf $HOME/stuff/recording.mp4
    geometry=$(slurp)
    notify-send "Screen recording in 3 seconds..."
    sleep 3
    notify-send "Screen recording started"
    wf-recorder -g "$geometry"
    mv recording.mp4 $HOME/stuff/
elif [ "$choice" = "Stop screencast" ]; then
    pkill -SIGINT wf-recorder
    notify-send "Screen recording saved to ~/stuff/recording.mp4"
else 
    exit 1
fi
